﻿using MediatR;
using Traceability.API.Applications.Command.TracebackCommand;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.API.Applications.CommandHandler.TracebackCommandHandler
{
    public class TracebackDelCommandHandler : IRequestHandler<TracebackDelCommand, APIResult<int>>
    {
        private readonly IBaseRepository<Traceback_Information> tracebackRepository;

        public TracebackDelCommandHandler(IBaseRepository<Traceback_Information> tracebackRepository)
        {
            this.tracebackRepository = tracebackRepository;
        }

        public async Task<APIResult<int>> Handle(TracebackDelCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> aPIResult = new APIResult<int>();
           var res= tracebackRepository.GetAll().FirstOrDefault(s=>s.Id==request.Id);
            if (res==null)
            {
                aPIResult.Code=EnumCode.File;
                aPIResult.Msg = "删除失败";
               return await Task.FromResult(aPIResult);

            }
            res.IsDeltede = true;
            await tracebackRepository.Update(res);
            aPIResult.Code = EnumCode.Ok;
            aPIResult.Msg = "删除成功";
            return await Task.FromResult(aPIResult);
        }
    }
}
